#include "AiboServer.h"

AiboServer::AiboServer(QObject *parent) :
    QObject(parent)
{
    /* menu */
    int portMenu = 10020;
    m_serverMenu = new QTcpServer(this);
    if (!m_serverMenu->listen(QHostAddress::LocalHost, (quint16) portMenu)) {
        qDebug() << "Aibo Server Menu unable to start: " << m_serverMenu->errorString();
        m_serverMenu->close();
        return;
    }
    connect(m_serverMenu, SIGNAL(newConnection()), this, SLOT(serverMenuConnection()));
    qDebug() << "Aibo Server Menu listening on port" << (quint16) portMenu;

    /* walk */
    int portWalk = 10050;
    m_serverWalk = new QTcpServer(this);
    if (!m_serverWalk->listen(QHostAddress::LocalHost, (quint16) portWalk)) {
        qDebug() << "Aibo Server Walk unable to start: " << m_serverWalk->errorString();
        m_serverWalk->close();
        return;
    }
    connect(m_serverWalk, SIGNAL(newConnection()), this, SLOT(serverWalkConnection()));
    qDebug() << "Aibo Server Walk listening on port" << (quint16) portWalk;

    /* head */
    int portHead = 10052;
    m_serverHead = new QTcpServer(this);
    if (!m_serverHead->listen(QHostAddress::LocalHost, (quint16) portHead)) {
        qDebug() << "Aibo Server Head unable to start: " << m_serverHead->errorString();
        m_serverHead->close();
        return;
    }
    connect(m_serverHead, SIGNAL(newConnection()), this, SLOT(serverHeadConnection()));
    qDebug() << "Aibo Server Head listening on port" << (quint16) portHead;

    /* behavior */
    int portBehavior = 10053;
    m_serverBehavior = new QTcpServer(this);
    if (!m_serverBehavior->listen(QHostAddress::LocalHost, (quint16) portBehavior)) {
        qDebug() << "Aibo Server Behavior unable to start: " << m_serverBehavior->errorString();
        m_serverBehavior->close();
        return;
    }
    connect(m_serverBehavior, SIGNAL(newConnection()), this, SLOT(serverBehaviorConnection()));
    qDebug() << "Aibo Server Behavior listening on port" << (quint16) portBehavior;

    /* joint monitor */
    int portJointMonitor = 20002;
    m_serverJointMonitor = new QTcpServer(this);
    if (!m_serverJointMonitor->listen(QHostAddress::LocalHost, (quint16) portJointMonitor)) {
        qDebug() << "Aibo Server Joint Monitor unable to start: " << m_serverJointMonitor->errorString();
        m_serverJointMonitor->close();
        return;
    }
    connect(m_serverJointMonitor, SIGNAL(newConnection()), this, SLOT(serverJointMonitorConnection()));
    qDebug() << "Aibo Server Joint Monitor listening on port" << (quint16) portJointMonitor;

}

void AiboServer::serverMenuConnection()
{
    qDebug() << "*** New client connection on server menu ***";

    m_clientMenu = m_serverMenu->nextPendingConnection();

    connect(m_clientMenu, SIGNAL(readyRead()), this, SLOT(clientMenuMessage()));
}

void AiboServer::serverWalkConnection()
{
    qDebug() << "*** New client connection on server walk ***";

    m_clientWalk = m_serverWalk->nextPendingConnection();

    connect(m_clientWalk, SIGNAL(readyRead()), this, SLOT(clientWalkMessage()));
}

void AiboServer::serverHeadConnection()
{
    qDebug() << "*** New client connection on server head ***";

    m_clientHead = m_serverHead->nextPendingConnection();

    connect(m_clientHead, SIGNAL(readyRead()), this, SLOT(clientHeadMessage()));
}

void AiboServer::serverBehaviorConnection()
{
    qDebug() << "*** New client connection on server behavior ***";

    m_clientBehavior = m_serverBehavior->nextPendingConnection();

    connect(m_clientBehavior, SIGNAL(readyRead()), this, SLOT(clientBehaviorMessage()));
}

void AiboServer::serverJointMonitorConnection()
{
    qDebug() << "*** New client connection on server joint monitor ***";

    m_clientJointMonitor = m_serverJointMonitor->nextPendingConnection();
    QByteArray msg = m_clientJointMonitor->read(1000);

    connect(m_clientJointMonitor, SIGNAL(readyRead()), this, SLOT(clientJointMonitorMessage()));
}

void AiboServer::clientMenuMessage()
{
    QByteArray msg = m_clientMenu->read(1000);
    qDebug() << "*** New client message on server menu ***";
    qDebug() << "Msg:" << msg;
    qDebug() << "*****************************************";
}

void AiboServer::clientWalkMessage()
{
    QByteArray msg = m_clientWalk->read(1000);
    qDebug() << "*** New client message on server menu ***";
    qDebug() << "Msg:" << msg;
    qDebug() << "*****************************************";
}

void AiboServer::clientHeadMessage()
{
    QByteArray msg = m_clientHead->read(1000);
    qDebug() << "*** New client message on server menu ***";
    qDebug() << "Msg:" << msg;
    qDebug() << "*****************************************";
}

void AiboServer::clientBehaviorMessage()
{
    QByteArray msg = m_clientBehavior->read(1000);
    qDebug() << "*** New client message on server menu ***";
    qDebug() << "Msg:" << msg;
    qDebug() << "*****************************************";
}

void AiboServer::clientJointMonitorMessage()
{
    QByteArray msg = m_clientJointMonitor->read(1000);
    qDebug() << "*** New client message on server menu ***";
    qDebug() << "Msg:" << msg;
    qDebug() << "*****************************************";
}
